package com.lihq.scannertool.util;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.lihq.scannertool.DecodeCallBack;
import com.seuic.scanner.DecodeInfo;
import com.seuic.scanner.DecodeInfoCallBack;
import com.seuic.scanner.Scanner;
import com.seuic.scanner.ScannerFactory;
import com.seuic.scanner.ScannerKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

public class ActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks,DecodeInfoCallBack {

    private static final String TAG = "ActivityLifecycle";
    //判断activity是否在前台
    private List<Activity> mActivities = new ArrayList<>();
    private static Scanner mScanner;
    private Context mContext;
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            int ret1 = ScannerKey.open();
            if (ret1 > -1) {
                while (true) {
                    int ret = ScannerKey.getKeyEvent();
                    if (ret > -1) {
                        switch (ret) {
                            case ScannerKey.KEY_DOWN:
                                mScanner.startScan();
                                break;
                            case ScannerKey.KEY_UP:
                                mScanner.stopScan();
                                break;
                        }
                    }
                }
            }
        }
    };

    public ActivityLifecycleCallbacks(Context context) {
//        Log.e(TAG,"ActivityLifecycleCallbacks");
        mScanner = ScannerFactory.getScanner(context);
        mContext = context;

        new Thread(runnable).start();
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        Log.e(TAG,activity+"onCreate");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.e(TAG, "onActivityStarted" );
    }

    @Override
    public void onActivityResumed(Activity activity) {
        Log.e(TAG,"onActivityResumed");
        mActivities.add(activity);
        if(mActivities.size()==1) {
            //适应老版本枪
            if(Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP) {
                ScannerKey.open();
            }
            mScanner.setDecodeInfoCallBack(this);
            mScanner.open();
            mScanner.enable();
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
        Log.e(TAG, "onActivityPaused: ");
    }

    @Override
    public void onActivityStopped(Activity activity) {
        mActivities.remove(activity);
        if(mActivities.size()==0){
            mScanner.disable();
            mScanner.setDecodeInfoCallBack(null);
            mScanner.close();
            //适应老版本枪在退出程序后还可以继续扫描的bug
            if(Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP) {
                ScannerKey.close();
            }
        }
        Log.e(TAG, "onActivityStopped" );
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Log.e(TAG, "onActivityDestroyed: " );
    }

    @Override
    public void onDecodeComplete(DecodeInfo decodeInfo) {
        Log.e(TAG,"onDecodeComplete");
        for(int i =0 ; i<mActivities.size();i++){
            if(i==mActivities.size()-1){
               if(mActivities.get(i) instanceof DecodeCallBack) {
                   ((DecodeCallBack)mActivities.get(i)).onDecodeComplete(decodeInfo.barcode);
               }else{
                   return;
               }
            }else{
                Log.e(TAG,"backgroundActivity");
                return;
            }
        }
    }
}
